package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.MedicationSchedulingDTO;
import com.api.shopapi.entity.dto.MedicationSchedulingMorningDTO;
import com.api.shopapi.entity.dto.MedicationSchedulingNightDTO;
import com.api.shopapi.entity.dto.MedicationSchedulingNoonDTO;
import com.api.shopapi.service.*;
import com.api.shopapi.util.Util;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author maochuan
 */
@RestController
@RequestMapping("MedicationScheduling")
public class MedicationSchedulingController extends BaseController {

    @Autowired
    @Qualifier("MedicationSchedulingServiceImplOne")
    private MedicationSchedulingService medicationSchedulingService;

    @Autowired
    @Qualifier("MedicationSchedulingMorningServiceImplOne")
    private MedicationSchedulingMorningService medicationSchedulingMorningService;

    @Autowired
    @Qualifier("MedicationSchedulingMorningItemServiceImplOne")
    private MedicationSchedulingMorningItemService medicationSchedulingMorningItemService;

    @Autowired
    @Qualifier("MedicationSchedulingNoonServiceImplOne")
    private MedicationSchedulingNoonService medicationSchedulingNoonService;

    @Autowired
    @Qualifier("MedicationSchedulingNoonItemServiceImplOne")
    private MedicationSchedulingNoonItemService medicationSchedulingNoonItemService;

    @Autowired
    @Qualifier("MedicationSchedulingNightServiceImplOne")
    private MedicationSchedulingNightService medicationSchedulingNightService;

    @Autowired
    @Qualifier("MedicationSchedulingNightItemServiceImplOne")
    private MedicationSchedulingNightItemService medicationSchedulingNightItemService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String elderlyCode = jsonObject.getString("elderlyCode");
            String time = jsonObject.getString("time");
            Date times = getTimeString(time);
            JSONObject JOscheduling = jsonObject.getJSONObject("scheduling");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/MedicationScheduling/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            MedicationScheduling medicationScheduling = medicationSchedulingService.getByTimeAndElderlyCode(times,elderlyCode);

            if (medicationScheduling == null) {
                medicationScheduling = new MedicationScheduling();
                String medicationSchedulingCode = UUID.randomUUID().toString();
                medicationScheduling.setMedicationSchedulingCode(medicationSchedulingCode);
                medicationScheduling.setDay(getWeek(time));
                medicationScheduling.setTime(times);
                medicationScheduling.setElderlyCode(elderlyCode);
                medicationScheduling.setCreateTime(new Date());
                medicationScheduling.setStaffCode(staffCode);
                medicationScheduling.setInstitutionCode(institutionCode);
                medicationSchedulingService.insert(medicationScheduling,JOscheduling);
            } else {
                return failed("此日期老人用药已存在");
            }


            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/MedicationScheduling/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult update(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String elderlyCode = jsonObject.getString("elderlyCode");
            String medicationSchedulingCode = jsonObject.getString("medicationSchedulingCode");
            int status = jsonObject.getInteger("status");
            String time = jsonObject.getString("time");
            Date times = getTimeString(time);
            JSONObject JOscheduling = jsonObject.getJSONObject("scheduling");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/MedicationScheduling/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            MedicationScheduling medicationScheduling = medicationSchedulingService.getByTimeAndElderlyCode(times,elderlyCode);
            if(medicationScheduling!=null){
                if(!medicationScheduling.getMedicationSchedulingCode().equals(medicationSchedulingCode)){
                   return failed("此日期老人用药已存在");
                }
            }
            medicationScheduling.setElderlyCode(elderlyCode);
            medicationScheduling.setStatus(status);
            medicationScheduling.setDay(getWeek(time));
            medicationScheduling.setTime(times);
            medicationSchedulingService.updateByMedicationSchedulingCode(medicationScheduling,JOscheduling);
            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/MedicationScheduling/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult getList(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String elderlyName = jsonObject.getString("elderlyName");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/MedicationScheduling/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            endTime = Util.timeAddDay(endTime,1);
            PageInfo<MedicationSchedulingDTO> medicationSchedulingPageInfo = medicationSchedulingService.getPageList(pageIndex, pageSize,institutionCode,elderlyName,status, startTime, endTime);
            for (MedicationSchedulingDTO medicationSchedulingDTO : medicationSchedulingPageInfo.getList()){
                //查询早上
                MedicationSchedulingMorningDTO medicationSchedulingMorningDTO = medicationSchedulingMorningService.getByMedicationSchedulingCode(medicationSchedulingDTO.getMedicationSchedulingCode());
                if(medicationSchedulingMorningDTO!=null){
                    List<MedicationSchedulingMorningItem> medicationSchedulingMorningItemList = medicationSchedulingMorningItemService.getListByMedicationSchedulingMorningCode(medicationSchedulingMorningDTO.getMedicationSchedulingMorningCode());
                    medicationSchedulingMorningDTO.setMedicationSchedulingMorningItemList(medicationSchedulingMorningItemList);
                    medicationSchedulingDTO.setMedicationSchedulingMorningDTO(medicationSchedulingMorningDTO);
                }
                //查询中午
                MedicationSchedulingNoonDTO medicationSchedulingNoonDTO = medicationSchedulingNoonService.getByMedicationSchedulingCode(medicationSchedulingDTO.getMedicationSchedulingCode());
                if(medicationSchedulingNoonDTO!=null){
                    List<MedicationSchedulingNoonItem> medicationSchedulingNoonItemList = medicationSchedulingNoonItemService.getListByMedicationSchedulingNoonCode(medicationSchedulingNoonDTO.getMedicationSchedulingNoonCode());
                    medicationSchedulingNoonDTO.setMedicationSchedulingNoonItemList(medicationSchedulingNoonItemList);
                    medicationSchedulingDTO.setMedicationSchedulingNoonDTO(medicationSchedulingNoonDTO);
                }
                //查询晚上
                MedicationSchedulingNightDTO medicationSchedulingNightDTO = medicationSchedulingNightService.getByMedicationSchedulingCode(medicationSchedulingDTO.getMedicationSchedulingCode());
                if(medicationSchedulingNightDTO!=null){
                    List<MedicationSchedulingNightItem> medicationSchedulingNightItemList = medicationSchedulingNightItemService.getListByMedicationSchedulingNightCode(medicationSchedulingNightDTO.getMedicationSchedulingNightCode());
                    medicationSchedulingNightDTO.setMedicationSchedulingNightItemList(medicationSchedulingNightItemList);
                    medicationSchedulingDTO.setMedicationSchedulingNightDTO(medicationSchedulingNightDTO);
                }
            }
            return success(medicationSchedulingPageInfo);

        } catch (Exception e) {
            insertError("/MedicationScheduling/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String elderlyName = jsonObject.getString("elderlyName");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/MedicationScheduling/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            endTime = Util.timeAddDay(endTime,1);

            PageInfo<MedicationSchedulingDTO> medicationSchedulingPageInfo = medicationSchedulingService.pageByGovCode(pageIndex, pageSize, staff.getGovernmentCode());


            for (MedicationSchedulingDTO medicationSchedulingDTO : medicationSchedulingPageInfo.getList()){

                //查询早上
                MedicationSchedulingMorningDTO medicationSchedulingMorningDTO = medicationSchedulingMorningService.getByMedicationSchedulingCode(medicationSchedulingDTO.getMedicationSchedulingCode());
                if(medicationSchedulingMorningDTO!=null){
                    List<MedicationSchedulingMorningItem> medicationSchedulingMorningItemList = medicationSchedulingMorningItemService.getListByMedicationSchedulingMorningCode(medicationSchedulingMorningDTO.getMedicationSchedulingMorningCode());
                    medicationSchedulingMorningDTO.setMedicationSchedulingMorningItemList(medicationSchedulingMorningItemList);
                    medicationSchedulingDTO.setMedicationSchedulingMorningDTO(medicationSchedulingMorningDTO);
                }

                //查询中午
                MedicationSchedulingNoonDTO medicationSchedulingNoonDTO = medicationSchedulingNoonService.getByMedicationSchedulingCode(medicationSchedulingDTO.getMedicationSchedulingCode());
                if(medicationSchedulingNoonDTO!=null){
                    List<MedicationSchedulingNoonItem> medicationSchedulingNoonItemList = medicationSchedulingNoonItemService.getListByMedicationSchedulingNoonCode(medicationSchedulingNoonDTO.getMedicationSchedulingNoonCode());
                    medicationSchedulingNoonDTO.setMedicationSchedulingNoonItemList(medicationSchedulingNoonItemList);
                    medicationSchedulingDTO.setMedicationSchedulingNoonDTO(medicationSchedulingNoonDTO);
                }

                //查询晚上
                MedicationSchedulingNightDTO medicationSchedulingNightDTO = medicationSchedulingNightService.getByMedicationSchedulingCode(medicationSchedulingDTO.getMedicationSchedulingCode());
                if(medicationSchedulingNightDTO!=null){
                    List<MedicationSchedulingNightItem> medicationSchedulingNightItemList = medicationSchedulingNightItemService.getListByMedicationSchedulingNightCode(medicationSchedulingNightDTO.getMedicationSchedulingNightCode());
                    medicationSchedulingNightDTO.setMedicationSchedulingNightItemList(medicationSchedulingNightItemList);
                    medicationSchedulingDTO.setMedicationSchedulingNightDTO(medicationSchedulingNightDTO);
                }

            }

            return success(medicationSchedulingPageInfo);

        } catch (Exception e) {
            insertError("/MedicationScheduling/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


}
